require('./Approvalsug.styl');
const { Group, SelectField, TextField, TextareaField, Button, Boxs, Toast} = SaltUI;
const {HBox, Box} = Boxs;
const File = require('images/svg/file.svg');
const Photo = require('images/svg/photo.svg');

class Approvalsug extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            placeholder:"请输入同意理由（非必填）",
            file:[],
            image:[]
        };
    }

    handleTextChange(name, newValue) {
        this.setState({
          [name]: newValue
        });
    }

    handleClick(){
        let t = this;
        if(t.state.placeholder.indexOf('同意') == -1){
            if(!t.state.suggestion){
                Toast.show({
                    type: 'error',
                    content: "评论不能为空",
                  });
            }else{
                if(t.state.placeholder.indexOf('拒绝') > -1)
                    t.props.onsend({content:t.state.suggestion,suggestType:'拒绝',file:t.state.file,imgae:t.state.image})
                else
                    t.props.onsend({content:t.state.suggestion,suggestType:'评论',file:t.state.file,imgae:t.state.image})
            }
        }else{
            var suggestion = t.state.suggestion ? t.state.suggestion : ""
            t.props.onsend({content:suggestion,suggestType:'同意',file:t.state.file,image:t.state.image})
        }
    }
    
    render() {
        let t = this;
        return (
            <div className="approvalsug myform">
                <Group.List>
                    <TextareaField
                        minRows={7} maxRows={10}
                        placeholder={this.state.placeholder}
                        value={t.state.suggestion}
                        onChange={t.handleTextChange.bind(t, 'suggestion')}
                    />
                </Group.List>
                {/* <Group.List>
                    <HBox className="photoBox">
                        <Box flex={0.5}>
                            <TextField
                                label="上传图片"
                                readOnly={true}
                                />
                        </Box>
                        <Box flex={1}>
                            <form className="fileForm">
                                <input type="file" value="" className="fileInput" accept="image/*"/>
                            </form>
                            <Photo height="20" width="18" className="fileFormIcon"/>
                        </Box>
                    </HBox>
                    <HBox>
                        <Box flex={0.5}>
                            <TextField
                                label="上传附件"
                                readOnly={true}
                                />
                        </Box>
                        <Box flex={1}>
                            <form className="fileForm">
                                <input type="file" value="" name="file" className="fileInput" />
                            </form>
                            <File height="20" width="18" className="fileFormIcon"/>
                        </Box>
                    </HBox>
                </Group.List> */}
                {(window.isBp || DingTalkPC.ua.isDesktop) && <Button type="primary" loading={t.props.loading} className="submitBtn" onClick={t.handleClick.bind(t)}>发送</Button>}
            </div>
        );
    }

    componentWillMount() {
    }

    componentDidMount() {
        let t = this
        dd.biz.navigation.setRight({
            show: true,
            control: true,
            text: '发送',
            onSuccess : function(result) {
                t.handleClick()
            },
            onFail : function(err) {}
        });
    }

    componentWillReceiveProps(nextProps) {
        this.setState({
            placeholder: nextProps.placeholder
        })
    }

    shouldComponentUpdate(nextProps, nextState) {
        return true;
    }

    componentWillUpdate(nextProps, nextState) {
    }

    componentDidUpdate(prevProps, prevState) {
    }

    componentWillUnmount() {
        dd.biz.navigation.setRight({
            show: true,
            control: true,
            text: '更多',
            onSuccess : function(result) {
                
            },
            onFail : function(err) {}
        });
    }
}

module.exports = Approvalsug;
